package com.zzt.questionbankquestion.service.impl;

import com.zzt.questionbankquestion.bean.QuestionType;
import com.zzt.questionbankquestion.bean.QuestionTypeMode;
import com.zzt.questionbankquestion.bean.dto.QuestionTypeModeDTO;
import com.zzt.questionbankquestion.bean.dto.QuestionTypeModeListDTO;
import com.zzt.questionbankquestion.bean.dto.Result;
import com.zzt.questionbankquestion.bean.tools.BeanUtils;
import com.zzt.questionbankquestion.mapper.QuestionTypeModeCpnverter;
import com.zzt.questionbankquestion.repository.QuestionTypeModeRepository;
import com.zzt.questionbankquestion.repository.QuestionTypeRepository;
import com.zzt.questionbankquestion.service.QuestionTypeModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class QuestionTypeModeServiceImpl implements QuestionTypeModeService {
    @Autowired
    private QuestionTypeModeCpnverter questionTypeModeCpnverter;

    @Autowired
    private QuestionTypeModeRepository questionTypeModeRepository;

    @Autowired
    private QuestionTypeRepository questionTypeRepository;

    @Override
    public Result addQuestionTypeMode(QuestionTypeModeListDTO dto) {
        String check="1234";
        int checkSort=-1;
        Result result=new Result();
        QuestionType questionType=new QuestionType();
        if(questionTypeRepository.findByQuestionTypeName(dto.getQuestionTypeName())==null){
            questionType.setQuestionTypeName(dto.getQuestionTypeName());
            long id=questionTypeRepository.save(questionType).getId();
            List< QuestionTypeMode > dos=questionTypeModeCpnverter.dtoToDO(dto.getModes());
            int flag=1;
            for(QuestionTypeMode item:dos){
                if (!(item.getModeType().equals("message")||item.getModeType().equals("selection")||item.getModeType().equals("question")||item.getModeType().equals("answer"))){
                    questionTypeRepository.deleteById(id);
                    result.setCode(2);
                    result.setMsg("添加失败，参数有问题");
                    flag=0;
                    break;
                }
                if ((check.equals("question")&&!item.getModeType().equals("answer"))||(item.getModeType().equals("answer")&&!check.equals("question"))||item.getSort()-checkSort!=1){
                    System.out.println(1);
                    questionTypeRepository.deleteById(id);
                    result.setCode(2);
                    result.setMsg("添加失败，参数有问题");
                    flag=0;
                    break;
                }
                check=item.getModeType();
                checkSort=item.getSort();
            }
            if (flag==1){
                try {
                    for(QuestionTypeMode item:dos){
                        item.setQuestionTypeId(id);
                        questionTypeModeRepository.save(item);
                    }
                    result.setCode(1);
                    result.setMsg("添加成功");

                }catch (Exception e){
                    result.setCode(-1);
                    result.setMsg("添加失败");
                }
            }
        }
        else {
            result.setCode(4);
            result.setMsg("题型已存在");
        }
        return result;
    }

    @Override
    public Result deleteByType(Long questionTypeId) {
        Result result=new Result();
        try{
            questionTypeRepository.deleteById(questionTypeId);
            questionTypeModeRepository.deleteByQuestionTypeId(questionTypeId);
        }catch (Exception e){
        }
        result.setCode(1);
        result.setMsg("删除成功");
        return result;
    }

    @Override
    public Result< QuestionTypeModeListDTO > getByTypeId(Long questionTypeId) {
        Result< QuestionTypeModeListDTO > result=new Result<>();
        QuestionTypeModeListDTO questionTypeModeListDTO=new QuestionTypeModeListDTO();
        List<QuestionTypeMode> dos=questionTypeModeRepository.findByQuestionTypeId(questionTypeId);
        if(dos.size()>0){
            List<QuestionTypeModeDTO> dtos=questionTypeModeCpnverter.doToDTO(dos).stream().sorted((a, b) -> (a.getSort() - b.getSort()))
                    .collect(Collectors.toList());
            questionTypeModeListDTO.setModes(dtos);
            questionTypeModeListDTO.setQuestionTypeName(questionTypeRepository.findById(questionTypeId).get().getQuestionTypeName());
            result.setCode(1);
            result.setMsg("获取成功");
            result.setData(questionTypeModeListDTO);
        }
        else {
            result.setCode(4);
            result.setMsg("不存在题型或对应的模块");
        }
        return result;
    }
}
